using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
#if !SILVERLIGHT
using System.Security;
#endif
using System.Text;

namespace Gko.Utils
{
	/// <summary>
	/// 	Extenstions class tor string
	/// </summary>
	public static class StringExtensions
	{
#if !SILVERLIGHT
	/// <summary>
	///		Converts string to the secure string.
	/// </summary>
	/// <param name="value">The string to convert.</param>
	/// <returns>Secure string</returns>
	/// <exception cref="System.ArgumentNullException">Value is <c>null</c></exception>
		public static SecureString ToSecureString(this string value)
		{
			if (value.IsNull()) {
				throw new ArgumentNullException("value");
			}
			var ss = new SecureString();
			foreach (var c in value) {
				ss.AppendChar(c);
			}
			return ss;
		}

#endif

		/// <summary>
		/// 	Indicates whether the specified <see cref = "T:System.String" /> object is <c>null</c> or an <see cref = "F:System.String.Empty" /> string.
		/// </summary>
		/// <param name = "value">A <see cref = "T:System.String" /> reference</param>
		/// <returns>
		/// 	<c>true</c> if the value parameter is <c>null</c> or an empty string (""); otherwise, <c>false</c>.
		/// </returns>
		public static bool IsNullOrEmpty(this string value)
		{
			return String.IsNullOrEmpty(value);
		}

		/// <summary>
		/// 	Replaces the format item in a specified <see cref = "T:System.String" /> with the text equivalent.
		/// </summary>
		/// <param name = "format">A composite format string.</param>
		/// <param name = "args">An <see cref = "T:System.Object" /> array containing zero or more objects to format.</param>
		/// <returns>A copy of format in which the format items have been replaced by the <see cref = "T:System.String" /> equivalent of the corresponding instances of <see cref = "T:System.Object" /> in args</returns>
		[SuppressMessage("Microsoft.Naming", "CA1719:ParameterNamesShouldNotMatchMemberNames", MessageId = "0#")]
		[SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.String.Format(System.String,System.Object[])")]
		public static String Format(this string format, params object[] args)
		{
			if (format == null) {
				throw new ArgumentNullException("format");
			}
			return String.Format(format, args);
		}

		/// <summary>
		/// 	Replaces the format item in a specified <see cref = "T:System.String" /> with the text equivalent.
		/// </summary>
		/// <param name = "format">A composite format string.</param>
		/// <param name = "arg0">An <see cref = "T:System.Object" /> to format.</param>
		/// <returns>
		/// 	A copy of format in which the format items have been replaced by the <see cref = "T:System.String" /> equivalent of the corresponding instances of <see cref = "T:System.Object" /> in args
		/// </returns>
		[SuppressMessage("Microsoft.Naming", "CA1719:ParameterNamesShouldNotMatchMemberNames", MessageId = "0#")]
		[SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.String.Format(System.String,System.Object)")]
		public static String Format(this string format, object arg0)
		{
			if (format == null) {
				throw new ArgumentNullException("format");
			}
			return String.Format(format, arg0);
		}

		/// <summary>
		/// 	Replaces the format item in a specified <see cref = "T:System.String" /> with the text equivalent.
		/// </summary>
		/// <param name = "format">A composite format string.</param>
		/// <param name = "arg0">An <see cref = "T:System.Object" /> to format.</param>
		/// <param name = "arg1">The second <see cref = "T:System.Object" /> to format.</param>
		/// <returns>
		/// 	A copy of format in which the format items have been replaced by the <see cref = "T:System.String" /> equivalent of the corresponding instances of <see cref = "T:System.Object" /> in args
		/// </returns>
		[SuppressMessage("Microsoft.Naming", "CA1719:ParameterNamesShouldNotMatchMemberNames", MessageId = "0#")]
		[SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.String.Format(System.String,System.Object,System.Object)")]
		public static String Format(this string format, object arg0, object arg1)
		{
			if (format == null) {
				throw new ArgumentNullException("format");
			}
			return String.Format(format, arg0, arg1);
		}

		/// <summary>
		/// 	Replaces the format item in a specified <see cref = "T:System.String" /> with the text equivalent.
		/// </summary>
		/// <param name = "format">A composite format string.</param>
		/// <param name = "arg0">An <see cref = "T:System.Object" /> to format.</param>
		/// <param name = "arg1">The second <see cref = "T:System.Object" /> to format.</param>
		/// <param name = "arg2">The third <see cref = "T:System.Object" /> to format.</param>
		/// <returns>
		/// 	A copy of format in which the format items have been replaced by the <see cref = "T:System.String" /> equivalent of the corresponding instances of <see cref = "T:System.Object" /> in args
		/// </returns>
		[SuppressMessage("Microsoft.Naming", "CA1719:ParameterNamesShouldNotMatchMemberNames", MessageId = "0#")]
		[SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.String.Format(System.String,System.Object,System.Object,System.Object)")]
		public static String Format(this string format, object arg0, object arg1, object arg2)
		{
			if (format == null) {
				throw new ArgumentNullException("format");
			}
			return String.Format(format, arg0, arg1, arg2);
		}

		/// <summary>
		/// 	Replaces the format item in a specified <see cref = "T:System.String" /> with the text equivalent of the value of a corresponding <see cref = "T:System.Object" /> instance in a specified array. A specified parameter supplies culture-specific formatting information.
		/// </summary>
		/// <param name = "format">A composite format string.</param>
		/// <param name = "provider">An <see cref = "T:System.IFormatProvider" /> that supplies culture-specific formatting information.</param>
		/// <param name = "args">An <see cref = "T:System.Object" /> array containing zero or more objects to format.</param>
		/// <returns>A copy of format in which the format items have been replaced by the <see cref = "T:System.String" /> equivalent of the corresponding instances of <see cref = "T:System.Object" /> in args</returns>
		[SuppressMessage("Microsoft.Naming", "CA1719:ParameterNamesShouldNotMatchMemberNames", MessageId = "0#")]
		public static String Format(this string format, IFormatProvider provider, params object[] args)
		{
			if (format == null) {
				throw new ArgumentNullException("format");
			}
			return String.Format(provider, format, args);
		}

		/// <summary>
		/// 	Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object.
		/// </summary>
		/// <typeparam name = "TEnum">The type of the enum.</typeparam>
		/// <param name = "value">A string containing the name or value to convert.</param>
		/// <returns>An object of type TEnum whose value is represented by value</returns>
		[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter")]
		public static TEnum ParseEnum<TEnum>(this string value) where TEnum : struct
		{
#if SILVERLIGHT
			return value.ParseEnum<TEnum>(false);
#else
			return (TEnum)Enum.Parse(typeof(TEnum), value);
#endif
		}

		/// <summary>
		/// 	Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object. A parameter specifies whether the operation is case-sensitive.
		/// </summary>
		/// <typeparam name = "TEnum">The type of the enum.</typeparam>
		/// <param name = "value">The value.</param>
		/// <param name = "ignoreCase">If <c>true</c>, ignore case; otherwise, regard case.</param>
		/// <returns>An object of type TEnum whose value is represented by value</returns>
		[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter")]
		public static TEnum ParseEnum<TEnum>(this string value, bool ignoreCase) where TEnum : struct
		{
			return (TEnum)Enum.Parse(typeof(TEnum), value, ignoreCase);
		}

		/// <summary>
		/// 	Initializes a new instance of the System.Uri class with the specified URI.
		/// </summary>
		/// <param name = "value">The URI string.</param>
		/// <returns>New instance of <see cref = "T:System.Uri" /> class</returns>
		[SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "0#")]
		public static Uri ToUri(this string value)
		{
			return new Uri(value);
		}

		/// <summary>
		/// 	Initializes a new instance of the System.Uri class with the specified URI. This constructor allows you to specify if the URI string is a relative URI, absolute URI, or is indeterminate.
		/// </summary>
		/// <param name = "value">A string that identifies the resource to be represented by the System.Uri instance.</param>
		/// <param name = "uriKind"> Specifies whether the URI string is a relative URI, absolute URI, or is indeterminate.</param>
		/// <returns>New instance of <see cref = "T:System.Uri" /> class</returns>
		[SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "0#")]
		public static Uri ToUri(this string value, UriKind uriKind)
		{
			return new Uri(value, uriKind);
		}
	}
}
